<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
	<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
	<title>任务管理</title>
	<script type="text/javascript" src="../../js/boot.js"></script>
</head>

<body>
	<div style="width:100%;">
		<fieldset id="fd1" style="width: 97%; margin-bottom: 4px;">
			<legend><span>查询条件</span></legend>
			<div id="queryForm" class="fieldset-body">
				<table class="form-table" border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td class="form-label">
							<span>任务名称：</span>
							<input name="projectName" class="mini-textbox" style="width: 150px" />
							<input name="projectName_link" value="like" class="mini-hidden" style="width: 150px" />
							<a class="mini-button" onclick="doSearch">查询</a>
							<a class="mini-menubutton " menu="#editPopupMenu">编辑</a>
							<ul id="editPopupMenu" class="mini-menu" style="display:none;">
								<li onclick="doEdit('add')">新增</li>
								<li onclick="doEdit('update')">修改</li>
								<li onclick="doDelete()">删除</li>
							</ul>
							<a class="mini-menubutton " menu="#popupMenu">操作</a>
							<ul id="popupMenu" class="mini-menu" style="display:none;">
								<li onclick="startTask()">立即执行</li>
								<li onclick="viewLog()">查看日志</li>
							</ul>
						</td>
					</tr>
				</table>
			</div>
		</fieldset>

	</div>
	<div style="width:100%;height:86%;">
		<div class="mini-fit">
			<div id="datagrid" class="mini-datagrid" style="width:100%;height:100%;" pageSize="20" allowResize="true" contextMenu="#gridMenu">
			</div>
		</div>
	</div>

	<ul id="gridMenu" class="mini-contextmenu">
		<li name="edit" iconCls="icon-edit" onclick="doEdit('update')">修改</li>
		<li name="remove" iconCls="icon-remove" onclick="doDelete()">删除</li>
		<li name="execute" iconCls="icon-ok" onclick="startTask()">立即执行</li>
		<li name="viewLog" iconCls="icon-search" onclick="viewLog()">查看日志</li>
	</ul>

	<div id="detailWin" class="mini-window" title="" style="width:560px;height:360px;" showMaxButton="true" showCollapseButton="true" showShadow="true"
		showToolbar="true" showFooter="true" showModal="true" allowResize="true" allowDrag="true">
		<div id="detailForm" style="padding:10px;">
			<input name="tableName" class="mini-hidden" value="SYS_TASK">
			<table align="center">
				<tr>
					<td align="right">任务编号：</td>
					<td align="left"><input name="taskID" class="mini-textbox" width="320" enabled="false"></td>
				</tr>
				<tr>
					<td align="right">任务名称：</td>
					<td align="left">
						<input id="taskName" name="taskName" class="mini-combobox" valueField="taskName" textField="taskName" data="taskList" width="320">
					</td>
				</tr>
				<tr>
					<td align="right">任务类名：</td>
					<td align="left">
						<input id="className" name="className" class="mini-textbox" width="320" enabled="false">
					</td>
				</tr>
				<tr>
					<td align="right">任务说明：</td>
					<td align="left"><input name="description" class="mini-textbox" width="320"></td>
				</tr>
				<tr>
					<td align="right">定时执行时间：</td>
					<td align="left"><input id="excuteDateTime" name="excuteDateTime" class="mini-datepicker" format="yyyy-MM-dd HH:mm:ss" timeFormat="HH:mm:ss"
							showTime="true" showOkButton="true" showClearButton="false" width="320"></td>
				</tr>
				<tr>
					<td align="right">周期执行类型：</td>
					<td align="left"><input name="cycleType" textField="text" valueField="id" class="mini-combobox" data="cycleTypes" width="320"></td>
				</tr>
				<tr>
					<td align="right">周期执行间隔：</td>
					<td align="left"><input name="cycleTime" class="mini-spinner" minValue="0" maxValue="59" width="320"></td>
				</tr>
				<tr>
					<td align="right">启动：</td>
					<td align="left"><input name="needStart" class="mini-checkbox" trueValue="1" falseValue="2" width="320"></td>
				</tr>
			</table>
		</div>
		<div property="footer" style="text-align:center;padding:5px;padding-right:15px;">
			<a class="mini-button" iconCls="icon-save" onClick="doSave()">保存</a>
			<a class="mini-button" iconCls="icon-cancel" onClick="cancel()">取消</a>
		</div>
	</div>
</body>

<script type="text/javascript">
	var cycleTypes = [
		{ id: -1, text: '执行一次' },
		{ id: 1, text: '重复执行' },
		{ id: 2, text: '每N秒执行' },
		{ id: 3, text: '每N分执行' },
		{ id: 4, text: '每N时执行' },
		{ id: 5, text: '每N天执行' },
		{ id: 6, text: '每N周执行' },
		{ id: 7, text: '每N月执行' }
	];

	var statusTypes = [
		{ id: 1, text: '启动' },
		{ id: 2, text: '停止' }
	];

	/* var allTaskClassName = [];
	http.getSync("/admin/task/getAllTaskClassName.do", function (page) {
		if (page.success) {
			var list = page.data;
			for (var i = 0; i < list.length; i++) {
				allTaskClassName.push({ id: list[i], text: list[i] });
			}
			console.log(allTaskClassName);
		} else {
			miniui.showErrorMessageBox(page.message);
		}
	}); */

	var taskList = [];
	http.getSync("/admin/task/getAllTaskClassName.do", function (page) {
		if (page.success) {
			taskList = page.data;
		} else {
			miniui.showErrorMessageBox(page.message);
		}
	});

	miniInit();
	var queryForm = new mini.Form("#queryForm");
	var detailForm = new mini.Form("#detailForm");
	var win = mini.get("detailWin");

	var datagrid = mini.get("datagrid");
	datagrid.set({
		url: http.getUrl("/com/map/page.do"),
		columns: [
			{ type: "indexcolumn" },
			{ field: "taskName", width: 100, headerAlign: "center", header: "任务名称" },
			{ field: "className", width: 100, headerAlign: "center", header: "任务执行类" },
			{ field: "excuteDateTime", width: 60, headerAlign: "center", header: "定时执行时间", dateFormat: "yyyy-MM-dd HH:mm:ss" },
			{ field: "cycleType", width: 60, headerAlign: "center", header: "周期执行类型", type: "comboboxcolumn", editor: { type: "combobox", data: cycleTypes } },
			{ field: "cycleTime", width: 50, headerAlign: "center", header: "周期执行间隔" },
			{ field: "needStart", width: 30, headerAlign: "center", header: "启动", type: "comboboxcolumn", editor: { type: "combobox", data: statusTypes } },
			{ field: "description", width: 100, headerAlign: "center", header: "任务说明" }
		]
	});

	datagrid.on("rowdblclick", function (e) {
		var record = e.record,
			column = e.column,
			field = e.field,
			value = e.value;
		var uid = record._uid;
		var row = datagrid.getRowByUID(uid);
		doEdit('update');
	});

	// 表单与表格绑定数据
	var db = new mini.DataBinding();
	db.bindForm("detailForm", datagrid);

	var cboTaskName = mini.get("taskName");
	var txtClassName = mini.get("className");
	cboTaskName.on("valuechanged", function (e) {
		// debugger
		var row = e.selected;
		txtClassName.setValue(row.className);
		//alert(txtClassName.getValue());
		//alert(cboTaskName.getValue());
	});

	doSearch();
	function doSearch() {
		var data = queryForm.getData(true, false);
		data.tableName = "AD_TASK";
		datagrid.load(data, null, miniui.showLoadErrorMessageBox);
	}

	function doEdit(type) {
		// debugger
		if (type == 'add') {
			win.setTitle("新增");
			detailForm.reset();
		} else {
			if (datagrid.getSelected() == undefined) {
				mini.alert("请先选择要编辑的数据!", "提示", null);
				return;
			}
			win.setTitle("修改");
			detailForm.setData(datagrid.getSelected());
		}
		win.showAtPos('center', 'middle');
	}

	function doSave() {
		if (!detailForm.validate()) {
			return;
		}
		miniui.mask("数据保存中...");
		var data = detailForm.getData();
		var excuteDateTime = mini.get("excuteDateTime");
		data.excuteDateTime = excuteDateTime.getFormValue();
		data.tableName = "AD_TASK";
		http.post("/admin/task/save.do", data, function (page) {
			mini.unmask();
			win.hide();
			if (page.success) {
				miniui.showSuccessTip("保存成功");
				datagrid.reload();
			} else {
				miniui.showErrorMessageBox(page.message);
			}
		});
	}

	function doDelete() {
		if (datagrid.getSelected()) {
			mini.confirm("确定要删除选中的记录吗？", "确定？",
				function (action) {
					if (action == "ok") {
						var table = datagrid.getSelected();
						var data = {
							"tableName": "AD_TASK",
							"taskID": table.taskID
						};
						http.post("/admin/task/delete.do", data, function (page) {
							if (page.success) {
								miniui.showSuccessTip("删除成功");
								datagrid.removeRow(table);
							} else {
								miniui.showErrorMessageBox(page.message);
							}
						});
					}
				});
		} else {
			miniui.showErrorMessageBox("请先选择要删除的记录！");
		}
	}

	// 立即执行
	function startTask() {
		miniui.mask("数据操作中...");
		if (datagrid.getSelected() == undefined) {
			mini.alert("请先选择要执行的任务!", "提示", null);
			return;
		}
		var data = datagrid.getSelected();
		data.cycleType = -1;
		data.needStart = 1;
		data.excuteDateTime = null;
		http.post("/admin/task/startTask.do", data, function (page) {
			mini.unmask();
			win.hide();
			if (page.success) {
				miniui.showSuccessTip("启动任务成功");
				datagrid.reload();
			} else {
				miniui.showErrorMessageBox(page.message);
			}
		});
	}

	//查看日志
	function viewLog() {
		if (datagrid.getSelected() == undefined) {
			mini.alert("请先选择要执行的任务!", "提示", null);
			return;
		}
		var taskID = datagrid.getSelected().taskID;
		mini.open({
			url: ctx + '/admin/system/task_log_list.html',
			title: "任务执行日志",
			width: 1200,
			height: 800,
			onload: function () {
				var iframe = this.getIFrameEl();
				setTimeout(function () {
					iframe.contentWindow.setData(taskID);
				}, 300);
			},
			ondestroy: function (action) {
				/*if(action == "ok") {
					search();
				}*/
				search();
			}
		});
	}

	function cancel() {
		win.hide();
	}
</script>

</html>